<?php
declare (strict_types = 1);
namespace app\circle\model;

use app\common\model\{Attach, BaseModel};
use app\common\logic\Attach as AttachLogic;
use think\model\concern\SoftDelete;
use app\user\logic\user\Account as User;

/**
 * 圈子评论模型
 */
class Comment extends BaseModel
{
    use softDelete;
    /**
     * 表名
     * @var string
     */
    protected $name = 'circle_comment';

    /**
     * 定义字段类型
     * @var array
     */
    protected $type = [
        'id'            => 'integer',
        'circle_id'     => 'integer',
        'user_id'       => 'integer',
        'comment_id'    => 'integer',
        'attach'        => 'json',
        'reply_user_id' => 'integer',
        'create_time'   => 'integer',
    ];

    protected $hidden = ['delete_time'];
    protected $append = ['attach_url'];

    /**
     * 附件url获取器
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-05-14
     * @param  string $value [description]
     * @param  [type] $data  [description]
     * @return [type]        [description]
     */
    public function getAttachUrlAttr($value='', $data)
    {
        $url = array();
        if ( !isset($data['attach']) || !$data['attach']) return $url;
        $attach = json_decode($data['attach'], true);
        if ( !count($attach) ) return $url;
        if ( is_array($attach) ) {
            foreach ($attach as $id) {
                $attach = Attach::find($id);
                if ( $attach ) {
                    if ( app(AttachLogic::class)->isAudioOrVideo($attach) ) {
                        $urls   = array_column($attach->fileurl, null, 'definition');
                        $url[]  = $urls['OD']['play_url'];
                    } else {
                        $url[]  = $attach->fileurl??'';
                    }
                } else {
                    $url[]  = '';
                }
            }
        }
            
        return $url;
    }

    /**
     * 关联圈子用户ID
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-17
     * @return [type] [description]
     */
    public function circleUser()
    {
        return $this->belongsTo(Circle::class, 'circle_id')->bind(['circle_user_id' => 'user_id']);
    }

    /**
     * 关联用户头像和昵称
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-21
     * @return [type] [description]
     */
    public function userBase()
    {
        return $this->belongsTo(User::class, 'user_id')->with(['avatar_url'])->bind(['nick_name', 'avatar_url']);
    }

    /**
     * 关联回复用户昵称
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-21
     * @return [type] [description]
     */
    public function replyUserName()
    {
        return $this->belongsTo(User::class, 'reply_user_id')->bind(['reply_user_name' => 'nick_name']);
    }

    /**
     * 关联点赞
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-21
     * @return boolean [description]
     */
    public function like()
    {
        return $this->hasOne(Like::class, 'obj_id')->where('type', 2)->whereUserId(request()->mid);
    }
}
